import { createRouter, createWebHistory } from 'vue-router'
import { notification } from 'ant-design-vue';
import store from "@/store";

const routes = [
  {
    path: '/',
    name: 'main',
    component: () => import('../views/MainView.vue'),
    meta:{
      loginRequire:true
    },
    children:[
      {
        path: 'welcome',
        component: () => import('../views/main/WelcomeView.vue'),
      },
      {
        path: 'passenger',
        component: () => import('../views/main/PassengerView.vue'),
      },
      {
        path: 'ticket',
        component: () => import('../views/main/TicketView.vue'),
      },
      {
        path: 'order',
        component: () => import('../views/main/OrderView.vue'),
      },
      {
        path: 'my-ticket',
        component: () => import('../views/main/MyTicketView.vue'),
      },
  ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '',
    // 重定向到/welcome页面
    redirect: '/welcome'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由的拦截 
router.beforeEach((to, from, next) => {
  if(to.matched.some(function(item){
    console.log(item,"是否需要登陆校验：",item.meta.loginRequire || false)
    return item.meta.loginRequire
  })){
    const member = store.state.member
    console.log("页面开始校验：",member)
    if(!member.token){
      console.log("用户未登录或登录超时！")
      notification.error({ description: '用户未登录或登录超时！' });
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})

export default router
